package jp.ac.u_tokyo.dch.dao.data;

import java.util.ArrayList;
import java.util.List;

import jp.ac.u_tokyo.dch.core.db.CustomPreparedStatement;
import jp.ac.u_tokyo.dch.core.db.CustomResultSet;
import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.core.exceptions.DBException;
import jp.ac.u_tokyo.dch.model.db.DataFile;
import jp.ac.u_tokyo.dch.util.common.CommonDBConnection;
/**
* @version 1.1
*/
public class DataFileDao {

	/**
	 * DBコネクションオブジェクト
	 */
	private DBConnection dbconn = null;

	/**
	 * 生成者
	 * @param DBコネクションオブジェクト
	 */
	public DataFileDao(DBConnection dbconn){
		this.dbconn = dbconn;
	}


	/**
	 * データファイル情報を挿入する
	 * @param list データファイルリスト
	 * @param dataId データID
	 * @return 挿入件数情報
	 * @throws DBException 例外
	 */
	public int[] insertAll(List<DataFile> list, int dataId)  throws DBException {

		if (list == null) {
			return null;
		}

		CustomPreparedStatement ps = null;
		int result[] = null;
		try{
			String query = "INSERT INTO data_file " +
					"(dataid, " +
					"realfilename," +
					"uploadfilename," +
					"filename," +
					"thumbnail," +
					"contenttype)" +
					" VALUES (?,?,?,?,?,?)";

			ps = dbconn.prepareStatement(query);

			for (DataFile dataFile : list) {

				int index = 1;
				ps.setInt(index++, dataId);
				ps.setString(index++, dataFile.getRealFileName());
				ps.setString(index++, dataFile.getUploadFileName());
				ps.setString(index++, dataFile.getFileName());
				ps.setString(index++, dataFile.getThumnail());
				ps.setString(index++, dataFile.getContentType());
				ps.addBatch();
			}

			result = ps.executeBatch();
		}catch(DBException dbe){
			dbe.printStackTrace();
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps);
		}
		return result;

	}


	/**
	 * データファイル情報を削除する
	 * @param dataId データID
	 * @return 削除件数
	 * @throws DBException 例外
	 */
	public int delete(int dataId)  throws DBException {

		CustomPreparedStatement ps = null;
		int result = 0;
		try{

			String query = "DELETE FROM data_file WHERE dataid = ?";

			ps = dbconn.prepareStatement(query);
			ps.setInt(1, dataId);
			result = ps.executeUpdate();
		}catch(DBException dbe){
			dbe.printStackTrace();
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps);
		}
		return result;

	}

	/**
	 * データファイルリスト取得
	 * @param dataId データID
	 * @return データファイルリスト
	 * @throws DBException 例外
	 */
	public List<DataFile> selectById(String dataId)  throws DBException {

		List<DataFile> list = new ArrayList<DataFile>();

		StringBuilder sqlQuery = new StringBuilder();
		sqlQuery.append("SELECT * FROM data_file WHERE dataid = ?");

		CustomPreparedStatement ps = null;
		CustomResultSet rs = null;
		try{
			ps = dbconn.prepareStatement(sqlQuery.toString());
			ps.setString(1, dataId);
			rs = ps.executeQuery();

			while (rs.next()) {

				DataFile dataFile = new DataFile();

				dataFile.setId(rs.getInt("id"));
				dataFile.setDataId(rs.getInt("dataid"));
				dataFile.setRealFileName(rs.getString("realfilename"));
				dataFile.setUploadFileName(rs.getString("uploadfilename"));
				dataFile.setFileName(rs.getString("filename"));
				dataFile.setThumnail(rs.getString("thumbnail"));
				dataFile.setContentType(rs.getString("contenttype"));
				list.add(dataFile);
			}

		}catch(DBException dbe){
			throw dbe;
		}finally{
			CommonDBConnection.closeResource(ps, rs);
		}
		return list;

	}

}
